﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Public;

namespace DAL
{
    public class DonViTinh_DAL
    {
        Connection conn = new Connection();
        public DataTable LayDSDVT()
        {
            return conn.LoadData("Select * from DONVITINH");
        }

        #region "Them Don Vi Tinh"
        public int ThemSuaDonViTinh(DonViTinh_PB dvt_pb)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvitinh"; value[0] = DonViTinh_PB.MaDonViTinh;
            name[1] = "@mota"; value[1] = DonViTinh_PB.DienGiai;
            string sql = @"if exists (select * from DONVITINH where MADONVITINH = @madonvitinh)
                             UPDATE [DONVITINH]
                             SET [DIENGIAI]	= @mota
                             WHERE  MADONVITINH		= @madonvitinh 
	                         else
                             INSERT INTO [DONVITINH]
                                        ([MADONVITINH]
                                        ,[DIENGIAI])
                                  VALUES (@madonvitinh,@mota)
                            ";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Sua Don Vi Tinh"
        public int SuaDonViTinh(DonViTinh_PB dvt_pb)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvitinh"; value[0] = DonViTinh_PB.MaDonViTinh;
            name[1] = "@mota"; value[1] = DonViTinh_PB.DienGiai;

            string sql = "update DONVITINH set DIENGIAI =  @mota  where  MADONVITINH = @madonvitinh";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Xoa Don Vi Tinh"
        public int XoaDonViTinh(DonViTinh_PB dvt_pb)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvitinh"; value[0] = DonViTinh_PB.MaDonViTinh;
            name[1] = "@mota"; value[1] = DonViTinh_PB.DienGiai;

            string sql = "delete from DONVITINH where MADONVITINH = @madonvitinh";
            return conn.Update(sql, name, value, nparameter);
        }
        public int XoaDonViTinh(string madonvitinh)
        {
            int nparameter = 1;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvitinh"; value[0] = madonvitinh;

            string sql = "delete from DONVITINH where MADONVITINH = @madonvitinh";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion
        public bool Exists(string ma)
        {
            string sql = string.Format("select * from DONVITINH where MADONVITINH = '{0}'", ma);
            DataTable dt = conn.LoadData(sql);
            if (dt == null) return false;

            return dt.Rows.Count > 0;
        }
    }

}
